package com.zt.rongda.quota.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zt.rongda.quota.model.QuotaConfig;
import com.zt.rongda.quota.service.QuotaConfigService;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/quotaConfig")
public class QuotaConfigController {

    @Autowired
    private QuotaConfigService quotaConfigService;

    @RequestMapping(value = "/getQuotaConfigList")
    @ResponseBody
    
    public JSONObject getQuotaConfigList(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(quotaConfigService.getQuotaConfigList(reqBean));
    }
    
    @RequestMapping(value = "/saveQuotaConfig")
    @ResponseBody
    
    public JSONObject saveQuotaConfig(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        quotaConfigService.saveQuotaConfig(reqBean);
        return Response.responseSuccess();
    }
    
    @RequestMapping(value = "/add")
    @ResponseBody
    
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        quotaConfigService.add(getQuotaConfig(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        quotaConfigService.update(getQuotaConfig(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        quotaConfigService.delete(reqBean.getInfo("quotaConfigId"));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private QuotaConfig getQuotaConfig(ReqBean reqBean) {
        QuotaConfig quotaConfig = new QuotaConfig();
        quotaConfig.setQuotaConfigId(reqBean.getInfo("quotaConfigId"));
        quotaConfig.setQuotaConfigParentId(reqBean.getInfo("quotaConfigParentId"));
        quotaConfig.setQuotaConfigName(reqBean.getInfo("quotaConfigName"));
        quotaConfig.setPublishTypeId(reqBean.getInfo("publishTypeId"));
        quotaConfig.setMakeTypeId(reqBean.getInfo("makeTypeId"));
        quotaConfig.setQuota(reqBean.getInfoDouble("quota"));
        quotaConfig.setOperator(reqBean.getUserId());
        quotaConfig.setRemark(reqBean.getInfo("remark"));
        return quotaConfig;
    }

}